package logic;

import interfaces.Observer;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

import constants.MessageType;

import models.Mushroom;
import models.PhysicalObject;
import models.Pig;
import models.PoisonMushroom;
import models.SparkleMushroom;
import models.Wolf;

import android.graphics.Rect;
import android.util.Log;
import android.util.Pair;

/**
 * 
 * @author Merete, Mirna, Esben, Knut og Stian
 * 
 */
public class CollisionHandler extends Observable {

	private static String TAG = CollisionHandler.class.getSimpleName();
	private List<PhysicalObject> list;
	private Pig pig;
	private Rect tempRect = new Rect();

	// 0 = rectinteserct, 1 = rectintersect + pixelcheck
	private int colCheckType = 1;

	private boolean loopBreaker = false;;
	Pair<PhysicalObject, MessageType> pair;

	public CollisionHandler(List<PhysicalObject> list) {
		this.list = list;
		if (list.get(0) instanceof Pig) {
			pig = (Pig) list.get(0);
		} else {
			pig = null;
		}
	}

	public boolean isTransparantPixel(PhysicalObject o, int x, int y) {
		if (o.getBitmap().getPixel((int) (x - (int) o.getHorizontalPosition()),
				(y - (int) o.getVerticalPosition())) != 0) {
			return false;
		}
		return true;
	}

	public boolean checkCollisions() {
		
		// check collision with other objects:
		for (PhysicalObject o : list) {
			if (list.isEmpty())
				break;

			if (o instanceof Mushroom && o.isActive()
					&& Rect.intersects(pig.getRect(), o.getRect())) {
				if (colCheckType == 0) {
					setChanged();
					if (o instanceof PoisonMushroom) {
						notifyObservers(MessageType.POISONMUSHROOMHIT);

					} else if (o instanceof SparkleMushroom) {
						notifyObservers(MessageType.SPARKLEMUSHROOMHIT);

					}
					o.setActive(false);

				} else if (colCheckType == 1) {
					// Makes a new rect containing the area of the interect:
					tempRect.set(pig.getRect().left, pig.getRect().top,
							pig.getRect().right, pig.getRect().bottom);
					tempRect.intersect(o.getRect());

					for (int x = tempRect.left; x < tempRect.right; x++) {

						if (loopBreaker) {
							break;
						}

						for (int y = tempRect.top; y < tempRect.bottom; y++) {
							if (!isTransparantPixel(pig, x, y)
									&& !isTransparantPixel(o, x, y)) {
								if (o instanceof PoisonMushroom) {
									o.setActive(false);
									setChanged();
									pair = new Pair<PhysicalObject, MessageType>(
											o, MessageType.POISONMUSHROOMHIT);
									notifyObservers(pair);
									loopBreaker = true;
									break;
								}

								if (o instanceof SparkleMushroom) {

									o.setActive(false);
									setChanged();
									pair = new Pair<PhysicalObject, MessageType>(
											o, MessageType.SPARKLEMUSHROOMHIT);
									notifyObservers(pair);
									loopBreaker = true;
									break;

								}
								loopBreaker = true;
								break;

							}

						}
					}
					loopBreaker = false;

				} else if (colCheckType == 2) {

				}

			} else if (o instanceof Wolf && o.isActive()) {

				if (Rect.intersects(pig.getRect(), o.getRect())) {
					if (colCheckType == 0) {
						setChanged();
						notifyObservers(MessageType.COLLISION);
						pig.resetSpeedAndPos();
						return true;

					} else if (colCheckType == 1) {

						// Makes a new rect containing the area of the
						// interect:
						tempRect.set(pig.getRect().left, pig.getRect().top,
								pig.getRect().right, pig.getRect().bottom);
						tempRect.intersect(o.getRect());

						for (int x = tempRect.left; x < tempRect.right; x++) {
							for (int y = tempRect.top; y < tempRect.bottom; y++) {
								if (!isTransparantPixel(pig, x, y)
										&& !isTransparantPixel(o, x, y)) {

									setChanged();
									pair = new Pair<PhysicalObject, MessageType>(
											o, MessageType.COLLISION);
									notifyObservers(pair);
									pig.resetSpeedAndPos();
									return true;

								}
							}

						}

					}

				} else if (colCheckType == 2) {
				}

			}
		}

		return false;

	}

}
